package com.qf.service;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.qf.mapper.DtsStorageMapper;
import com.qf.pojo.DtsStorage;
import com.qf.util.AliyunOSSUtil;
import com.qf.util.UUID;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;
import org.springframework.util.StringUtils;

import java.util.Date;

/**
 * @author fy
 * @version V1.0
 * @Project workspace
 * @Package com.qf.service
 * @Description:
 * @Date 2022/6/21 11:31
 */
@Service
public class StorageServiceImpl implements StorageService {
    @Autowired
    private DtsStorageMapper storageMapper;

    //阿里云北京服务器地址
    @Value("${aliyun.oss.endpoint}")
    private String endpoint;

    //子账户账号
    @Value("${aliyun.oss.accessKeyId}")
    private String accessKeyId;

    //子账户密码
    @Value("${aliyun.oss.accessKeySecret}")
    private String accessKeySecret;

    //bucket桶空间名字
    @Value("${aliyun.oss.bucketName}")
    private String bucketName;


    @Override
    public IPage<DtsStorage> queryStoragePage(String key, String name, Integer page, Integer limit, String sort, String order) {
        //1.创建分页对象
        IPage<DtsStorage> iPage = new Page<>(page,limit);
        //2.封装查询对象
        QueryWrapper<DtsStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("deleted", false);
        //key不为空，校验
        if (!StringUtils.isEmpty(key)){
            queryWrapper.eq("key", key);
        }
        //name不为空，校验
        if (!StringUtils.isEmpty(name)){
            queryWrapper.like("name", name);
        }
        //排序
        if (!StringUtils.isEmpty(sort)&&!StringUtils.isEmpty(order)){
            //排序方式是否为升序
            if (order.equalsIgnoreCase("asc")){
                queryWrapper.orderByAsc(sort);
            }else {
                //降序
                queryWrapper.orderByDesc(sort);
            }

        }


        //3.查询并返回结果
        IPage<DtsStorage> resultPage = storageMapper.selectPage(iPage, queryWrapper);

        return resultPage;
    }

    @Override
    public DtsStorage add(String name, Integer size, String ext, byte[] content) throws Exception {

        //1.生成随机字符串，作为文件名，防止重名
        String str = UUID.randomUUID().toString();
        //2.使用随机生成的字符串+文件扩展名组成新的文件名，
        String filename = str+ext;

        //3.调用阿里云工具类，保存文件到阿里云oss服务器，返回保存路径
        String url = AliyunOSSUtil.uploadImage(endpoint, accessKeyId, accessKeySecret, bucketName, filename, content);

        //4.封装Storage实体类
        DtsStorage storage = new DtsStorage();
        storage.setDeleted(false);
        //保存到阿里云服务器的文件名
        storage.setKey(filename);
        //原文件名
        storage.setName(name);
        //创建时间
        storage.setAddTime(new Date());
        //修改时间
        storage.setUpdateTime(new Date());
        //文件类型, 也就是扩展名
        storage.setType(ext);
        //文件大小
        storage.setSize(size);
        //保存到阿里云oss服务器地址
        storage.setUrl(url);

        //5. 将storage对象保存到数据库表中
        storageMapper.insert(storage);
        return storage;
    }

    @Override
    public int update(DtsStorage storage) {
        //设置修改时间
        storage.setAddTime(new Date());
        int count = storageMapper.updateById(storage);
        return count;
    }

    @Override
    public void deleteByKey(String key) {
        //1.根据key调用阿里云工具类，删除阿里oss服务器中的文件
        AliyunOSSUtil.deleteFile(endpoint, accessKeyId, accessKeySecret, bucketName, key);
        //2.删除数据库中的数据
        QueryWrapper<DtsStorage> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq("`key`", key);
        storageMapper.delete(queryWrapper);
    }
}
